package cn.yshujia.repository;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Album;
import cn.yshujia.domain.vo.AlbumVO;
import cn.yshujia.domain.vo.LabelVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.mapper.AlbumMapper;
import cn.yshujia.transform.AlbumTransform;
import cn.yshujia.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2025/3/4
 * @description AlbumRepository
 */


@Repository
public class AlbumRepository {
	
	@Resource
	RedisTemplate<String, AlbumVO> redis;
	
	@Resource
	AlbumMapper mapper;
	
	@Resource
	LabelRepository labelRepository;
	
	// 文章列表根据标签查询
	private static final String VO = "album:";
	
	private static final String LIST_LABEL = "album:label:";
	
	private static final Duration DURATION = Duration.ofDays(3L);
	
	public AlbumVO selectById(Long id) {
		
		AlbumVO vo = redis.opsForValue().get(VO + id);
		if (null != vo) {
			return vo;
		}
		Album album = mapper.selectById(id);
		if (null != album) {
			vo = createVO(album);
			redis.opsForValue().set(VO + id, vo, DURATION);
		}
		return vo;
	}
	
	public PageVO<AlbumVO> pageByLabelId(PageDTO dto) {
		Map<String, Object> map = dto.getParam();
		if (ObjectUtils.isEmpty(map) || null == map.get("labelId")) {
			return new PageVO<>();
		}
		long labelId = Long.parseLong(map.get("labelId").toString());
		// 判断缓存中是否存在
		long start = (long) (dto.getCurrent() - 1) * dto.getSize();
		List<AlbumVO> list = redis.opsForList().range(LIST_LABEL + labelId, start, start + dto.getSize() - 1);
		
		if (!ObjectUtils.isEmpty(list)) {
			return PageUtils.getPage(dto, list, redis.opsForList().size(LIST_LABEL + labelId));
		}
		
		// 获取数据库数据
		QueryWrapper<Album> wrapper = PageUtils.createAlbumWrapper(dto);
		IPage<Album> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), wrapper);
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, createVOList(page.getRecords()), page.getTotal());
		}
		
		return new PageVO<>();
	}
	
	
	public AlbumVO createVO(Album album) {
		AlbumVO vo = AlbumTransform.entity2VO(album);
		LabelVO labelVO = labelRepository.get(album.getLabelId());
		if (null != labelVO) {
			vo.setLabel(labelVO);
		}
		return vo;
	}
	
	public List<AlbumVO> createVOList(List<Album> list) {
		if (ObjectUtils.isEmpty(list)) {
			return new ArrayList<>();
		}
		List<AlbumVO> voList = new ArrayList<>(list.size());
		for (Album album : list) {
			AlbumVO vo = createVO(album);
			voList.add(vo);
		}
		return voList;
	}
	
	@Async ("Task")
	public void set(List<AlbumVO> list, String key) {
		redis.opsForList().rightPushAll(key, list);
		redis.opsForList().getOperations().expire(key, DURATION);
	}
	
	@Async ("Task")
	public void del() {
		redis.delete(LIST_LABEL + "*");
	}
}
